home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / FixedPoint.h < prev    next >
Text File  |  1995-01-04  |  3KB  |  71 lines

  1. /* FixedPoint.h */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #ifndef Included_FixedPoint_h
  26. #define Included_FixedPoint_h
  27.  
  28. /* FixedPoint module depends on */
  29. /* MiscInfo.h */
  30. /* Audit */
  31. /* Debug */
  32. /* Definitions */
  33.  
  34. #define smallfixed_precision (12)
  35. typedef unsigned short smallfixedunsigned;
  36. typedef signed short smallfixedsigned;
  37. typedef signed long smallfixedextended;
  38. #define smallfixed2int(x) (x / (/*NB long unnecessary*/1 << smallfixed_precision))
  39. #define int2smallfixed(x) (x * (1 << smallfixed_precision))
  40. #define smallfixed2double(x) (((double)x) / (1 << smallfixed_precision))
  41. #define double2smallfixed(x) ((smallfixedsigned)((x) * (1 << smallfixed_precision)\
  42.                     + ((x >= 0) ? 0.5 : -0.5)))
  43.  
  44. #define smallfixedmult(x,y) ((smallfixed)(((x) * (y)) + \
  45.                     (1 << (smallfixed_precision - 1)) / (1 << smallfixed_precision)))
  46.  
  47.  
  48. #define largefixed_precision (24)
  49. typedef unsigned long largefixedunsigned;
  50. typedef signed long largefixedsigned;
  51. #define largefixed2int(x) (x / (1L << largefixed_precision))
  52. #define int2largefixed(x) (x * (1L << largefixed_precision))
  53. #define largefixed2double(x) (((double)x) / (1L << largefixed_precision))
  54. #define largefixed2single(x) (((float)x) / (1L << largefixed_precision))
  55. #define double2largefixed(x) ((largefixedsigned)((x) * (1L << largefixed_precision)\
  56.                     + ((x >= 0) ? 0.5 : -0.5)))
  57.  
  58. #define largefixedmult(x,y) (double2largefixed(largefixed2double(x)\
  59.                     * largefixed2double(y)))
  60.  
  61.  
  62. #define smallfixed2largefixed(x) ((largefixedsigned)x\
  63.                     * (1L << (largefixed_precision - smallfixed_precision)))
  64. #define largefixed2smallfixed(x) ((smallfixed)(x\
  65.                     / (1L << (largefixed_precision - smallfixed_precision))))
  66.  
  67.  
  68. #define roundtonearest(x) ((long)((x >= 0) ? (x + 0.5) : (x - 0.5)))
  69.  
  70. #endif
  71.